package host

import (
	"net/http"
	"strconv"
	"time"
)

func NewQueryHostRequest() *QueryHostRequest {
	return &QueryHostRequest{
		PageSize:   0,
		PageNumber: 5,
	}
}
func NewQueryHostFromHTTP(r *http.Request) *QueryHostRequest {
	req := NewQueryHostRequest()
	// query string
	qs := r.URL.Query()
	pzs := qs.Get("page_size")
	if pzs != "" {
		pzi, _ := strconv.Atoi(pzs)
		req.PageSize = uint64(pzi)
	}
	pns := qs.Get("page_number")
	if pns != "" {
		pni, _ := strconv.Atoi(pns)
		req.PageNumber = uint64(pni)
	}
	req.Keywords = qs.Get("kws")
	return req
}

type QueryHostRequest struct {
	PageSize   uint64 `json:"page_size,omitempty"`
	PageNumber uint64 `json:"page_num,omitempty"`
	Keywords   string `json:"kws"`
}

func (req *QueryHostRequest) GetPageNum() uint {
	return uint(req.PageNumber)
}

func (req *QueryHostRequest) OffSet() int64 {
	return int64((req.PageNumber - 1) * req.PageSize)
}

// Hos模型的定义

func NewHost() *Host {
	return &Host{
		Resource: &Resource{},
		Describe: &Describe{},
	}
}

type Host struct {
	//资源公共属性部分
	*Resource
	//资源独有属性
	*Describe
}

func (h *Host) Validate() error {

	return validate.Struct(h)
}

func (h *Host) InjectDefault() {
	if h.CreateAt == 0 {
		h.CreateAt = time.Now().UnixMilli()
	}
}

type Vendor int

const (
	//枚举的默认值
	PRIVATE_IDC Vendor = iota
	ALIYUN
	TXYUN
)

func NewHostSet() *HostSet {
	return &HostSet{
		Items: []*Host{},
	}
}

func (s *HostSet) Add(item *Host) {
	s.Items = append(s.Items, item)
}

type HostSet struct {
	Total int     `json:"total"`
	Items []*Host `json:"items"`
}

type Resource struct {
	Id          string            `json:"id" validate:"required"`
	Vendor      Vendor            `json:"vendor"`
	Region      string            `json:"region" validate:"required"`
	CreateAt    int64             `json:"create_at"`
	ExpireAt    int64             `json:"expire_at"`
	Type        string            `json:"type" validate:"required"`
	Name        string            `json:"name" validate:"required"`
	Description string            `json:"description"`
	Status      string            `json:"status"`
	Tags        map[string]string `json:"tags"`
	UpdateAt    int64             `json:"update_at"`
	SyncAt      int64             `json:"sync_at"`
	Account     string            `json:"account"`
	PublicIP    string            `json:"public_ip"`
	PrivateIP   string            `json:"private_ip"`
	PayType     int               `json:"pay_type"`
}
type Describe struct {
	Memory       string `json:"memory"`
	CPU          string `json:"cpu"`
	GPUAmount    int    `json:"gpu_amount"`
	GPUSpec      string `json:"gpu_spec"`
	OSType       string `json:"os_type"`
	OSName       string `json:"os_name"`
	SerialNumber string `json:"serial_number"`
}

type UpdataHostRequest struct {
	*Describe
}

type DeleteHostRequest struct {
	ResourceID string `json:"resource_id"`
}
